<?php
/**
 * Created by Engineer CuiLiwu.
 * Project: deal.
 * Date: 2018/5/18-9:35
 * License Hangzhou orce Technology Co., Ltd. Copyright © 2018
 */

namespace App\Http\Controllers\Admin\Article;
use App\Http\Controllers\Admin\BaseController;
use Illuminate\Http\Request;

class ArticleController extends BaseController
{
    protected $api_url = [
        'index' => ['get', 'article'],
        'show'  => ['get', 'article/%d'],
        'update'  => ['put', 'article/%d'],
        'store'  => ['post', 'article'],
        'delete'  => ['delete', 'article/%d'],
        'audit'  => ['put', 'article/audit/%d'],
        'recommend'  => ['put', 'article/recommend/%d'],
        'category'  => ['get', 'article/categorypid'],
        'categoryfid'  => ['post', 'article/categoryfid/%id'],
        'categoryChildren'  => ['post', 'article/categoryChildren/%d'],
    ];

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }
    /**
     * 后台-文章列表
     * */
    public function index(){

        //参数部分
        $search_params = $this->request->all();
        $webSearch = $search_params;
        $fid = $this->request->get('fid',false);
        if ($fid!=false) {
            $children = http_admin($this->api_url['categoryChildren'], [$fid])->send(['fid'=>$fid]);
            $search_params['fid'] = $children['data'];
        }


        $page = $this->request->get('page', 1);
        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'orderBy'=>'created_at',
            'sortedBy'=>'desc',
            'page' => $page
        ];

        $category = http_admin($this->api_url['category'])->send();

        //请求部分
        $ret = http_admin($this->api_url['index'])->send($params);
        //结果处理部分
        $data = paginate($this->request, $ret['data']);

        //回填搜索框值
        $data['search_params'] = $webSearch;
        $data['category']=$category['data']['fid'];

        return view('Admin.article.article', $data);
    }

    public function create(){
        $category = http_admin($this->api_url['category'])->send();
        $ret['category_pid']=$category['data']['pid'];
        $ret['category_fid']=$category['data']['fid'];

        return view('Admin.article.article_create', $ret);

    }

    public function store(){
        $data=$this->request->all();
        $data['auth']=session('admin_user_name');
        $ret = http_admin($this->api_url['store'])->send($data);
        return response()->json(['code'=>0,'message'=>'操作成功','data'=>$ret['data']]);

    }


    public function show($id){
        //请求部分
        $ret = http_admin($this->api_url['show'],[$id])->send();

        $category = http_admin($this->api_url['category'])->send();
        $ret['data']['category_pid']=$category['data']['pid'];
        $ret['data']['category_fid']=$category['data']['fid'];
        return view('Admin.article.article_edit', $ret['data']);

    }

    public function update($id){
        $data=$this->request->all();
        $ret = http_admin($this->api_url['update'],[$id])->send($data);

        return response()->json(['code'=>0,'message'=>'操作成功','data'=>$ret['data']]);

    }
    
    public function destroy($id){
        $ret = http_admin($this->api_url['delete'],[$id])->send();
        return redirect('/foradmin/article');
        // return response()->json(['code'=>0,'message'=>'删除成功','data'=>$ret['data']]);
    }

    /**
     * 展示
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function audit($id){
        //请求部分
        $data=$this->request->all(['audit']);
        $ret = http_admin($this->api_url['audit'],[$id])->send($data);
        return response()->json(['code'=>0,'message'=>'操作成功','data'=>$ret['data']]);
    }

    /**
     * 推荐
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function recommend($id){
        //请求部分
        $data=$this->request->all(['recommend']);
        $ret = http_admin($this->api_url['recommend'],[$id])->send($data);
        return response()->json(['code'=>0,'message'=>'操作成功','data'=>$ret['data']]);
    }

    public function categoryfid($id){
        $data['fid']=$id;
        $ret = http_admin($this->api_url['categoryfid'],[$id])->send($data);
        return response()->json(['code'=>0,'message'=>'操作成功','data'=>$ret['data']]);

    }
}